package com.eling.elcms.travel.service.impl;

import com.eling.elcms.core.dao.hibernate.search.SearchCondition;
import com.eling.elcms.core.service.impl.GenericManagerImpl;
import com.eling.elcms.travel.dao.ICarDispatchDao;
import com.eling.elcms.travel.model.Car;
import com.eling.elcms.travel.model.Car.CarStatus;
import com.eling.elcms.travel.model.CarDispatchBill;
import com.eling.elcms.travel.model.Driver;
import com.eling.elcms.travel.model.Driver.DriverStatus;
import com.eling.elcms.travel.service.ICarDispatchManager;
import com.eling.elcms.travel.service.ICarManager;
import com.eling.elcms.travel.service.IDriverManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CarDispatchManagerImpl extends GenericManagerImpl<CarDispatchBill, Long> implements ICarDispatchManager {

	ICarDispatchDao carDispatchDao;

	@Autowired
	private ICarManager carManager;

	@Autowired
	private IDriverManager driverManager;

	@Autowired
	public void setCarDispatchDao(ICarDispatchDao carDispatchDao) {
		this.dao = carDispatchDao;
		this.carDispatchDao = carDispatchDao;
	}

	@Override
	public CarDispatchBill saveCarBill(CarDispatchBill carDispatchBill) {

		//当为本单位车辆时
		if (carDispatchBill.getUnitsCar()) {
			
			// 当状态为"收车(Back)"收车时改变车和司机的状态
			if (carDispatchBill.getCarStatus().equals(com.eling.elcms.travel.model.CarDispatchBill.CarStatus.Back)) {
				// 改变车辆状态
				Car car = carManager.get(carDispatchBill.getCar().getPkCar());
				car.setCarStatus(CarStatus.Free);
				carManager.save(car);

				// 改变司机状态
				Driver driver = driverManager.get(carDispatchBill.getDriver().getPkDriver());
				driver.setDriverStatus(DriverStatus.Free);
				driverManager.save(driver);
			} else {
				// 当生成调度单，车辆司机为本单位时，改变司机和车辆的状态
				Car car = carManager.get(carDispatchBill.getCar().getPkCar());
				car.setCarStatus(CarStatus.Running);
				carManager.save(car);

				// 改变司机状态
				Driver driver = driverManager.get(carDispatchBill.getDriver().getPkDriver());
				driver.setDriverStatus(DriverStatus.Passage);
				driverManager.save(driver);
			}
		}
		// 当不是本单位车时
		return carDispatchDao.save(carDispatchBill);

	}

	@SuppressWarnings("unchecked")
	@Override
	public List<CarDispatchBill> sreachCarBill(SearchCondition str) {
		String sc = str.getS();
		CarDispatchBill carDispatchBill = new CarDispatchBill();
		@SuppressWarnings("rawtypes")
		List list = new ArrayList();
		List<CarDispatchBill> query = query(carDispatchBill);
		for (CarDispatchBill carDisBill : query) {

			if (carDisBill.getDriverName() != null && carDisBill.getDriverName().contains(sc)) {
				list.add(carDisBill);
				continue;
			} else if (carDisBill.getDriver() != null
					&& carDisBill.getDriver().getPersonalInfo().getName().contains(sc)) {
				list.add(carDisBill);
				continue;
			}
		}
		return list;
	}

	@Override
	public List<CarDispatchBill> queryAllRecord(CarDispatchBill cond) {
		return carDispatchDao.queryAllRecord(cond);
	}

}
